<template>
  <el-form ref="course" :model="course">
    <Sticky :class-name="'sub-navbar'">
      <el-button v-loading="loading" type="success" @click="submitForm">
        {{ hasCourse ? "编辑课程" : "创建课程" }}
      </el-button>
    </Sticky>
    <div class="detail-container">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="title">
            <MDinput
              v-model="course.courseName"
              :max-length="100"
              name="name"
              required
            >
              课程名
            </MDinput>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="title">
            <MDinput
              v-model="course.description"
              type="text"
              :max-length="100"
              name="name"
              required
            >
              课程描述
            </MDinput>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="课程图片">
            <el-upload
              class="avatar-uploader"
              action="#"
              :http-request="UploadImage"
              :show-file-list="false"
              accept="image/jpeg,image/png,image/jpg"
              :on-success="handleAvatarSuccess"
            >
              <img
                v-if="course.imageUrl"
                :src="course.imageUrl"
                class="avatar"
              >
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
    </div>
  </el-form>
</template>

<script>
import Sticky from '@/components/Sticky'
import MDinput from '@/components/MDinput'
import { createCourse, updateCourse, UploadImage, getCourse } from '@/api/course'

export default {
  components: {
    Sticky,
    MDinput
  },
  props: {
    hasCourse: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      loading: false,
      course: {
        course_id: '',
        courseName: '',
        description: '',
        imageUrl: ''
      },
      action: process.env.VUE_APP_BASE_API + '/course/imgUpload'
    }
  },
  beforeCreate() {
    getCourse().then(response => {
      if (response.data !== null) {
        // 有课程
        this.$router.push('/course/edit')
        this.course = response.data
      } else {
        // 没有课程
        this.$router.push('/course/create')
      }
    })
  },
  methods: {
    submitForm() {
      this.lodding = true
      const course = Object.assign({}, this.course)
      if (!this.hasCourse) {
        createCourse(course).then(response => {
          console.log('createCourse', response)
          this.loading = false
          this.$notify({
            title: '成功',
            message: response.msg,
            type: 'success',
            duration: 2000
          })
          this.$router.push('/course/edit')
        }).catch(() => {
          this.loading = false
        })
      } else {
        updateCourse(course).then(response => {
          console.log('updateCourse', response)
          this.loading = false
          this.$notify({
            title: '成功',
            message: response.msg,
            type: 'success',
            duration: 2000
          })
        }).catch(() => {
          this.loading = false
        })
      }
    },
    UploadImage(param) {
      const formData = new FormData()
      formData.append('file', param.file)
      UploadImage(formData).then(response => {
        console.log('上传图片成功')
        this.$notify({
          title: '成功',
          message: response.msg,
          type: 'success',
          duration: 2000 })
        this.course.imageUrl = response.msg
        // 但是我们上传成功了图片， fileList 里面的值却没有改变，还好有on-change指令可以使用
      }).catch(response => {
        console.log(response.msg)
        console.log('图片上传失败')
        this.$notify({
          title: '失败',
          message: response.msg,
          type: 'error',
          duration: 2000 })
      })
    },
    handleAvatarSuccess(res, file) {
      // 图片上传，成功返回图片地址
      this.course.imageUrl = URL.createObjectURL(file.raw)
    }
  }
}
</script>

<style>
.detail-container {
  padding: 40px 50px 20px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
